/*
 * Copyright (C) 2005-2008 Alfresco Software Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

 * As a special exception to the terms and conditions of version 2.0 of 
 * the GPL, you may redistribute this Program in connection with Free/Libre 
 * and Open Source Software ("FLOSS") applications as described in Alfresco's 
 * FLOSS exception.  You should have received a copy of the text describing 
 * the FLOSS exception, and it is also available here: 
 * http://www.alfresco.com/legal/licensing"
 */

package org.alfresco.extension.avmversions.impl;

import java.util.Date;
import java.util.List;

import org.alfresco.extension.avmversions.AVMPurgeVersionsService;
import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avm.VersionDescriptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Implementation of AVMVersionPurgeService.
 *
 * @author Peter Monks (peter.monks@alfresco.com)
 * @version $Id$
 */
public class AVMPurgeVersionsServiceImpl
    implements AVMPurgeVersionsService
{
    private final static Log log = LogFactory.getLog(AVMPurgeVersionsServiceImpl.class);
    
    private final static int LOWEST_REVISION_THATS_SAFE_TO_DELETE = 3;  // Note: AVM uses revisions 0, 1 and 2 internally - purging them causes all sorts of problems. 
    
    
    private final AVMService avmService;
    
    
    
    public AVMPurgeVersionsServiceImpl(final AVMService avmService)
    {
        // PRECONDITIONS
        assert avmService != null : "avmService must not be null.";
        
        // Body
        this.avmService = avmService;
    }
    

    /**
     * @see org.alfresco.extension.AVMPurgeVersionsService.AVMVersionPurgeService#purge(java.lang.String, int)
     */
    public void purge(String avmStoreId, int version)
    {
        // PRECONDITIONS
        assert avmStoreId != null && avmStoreId.trim().length() > 0 : "avmStoreId must not be null, empty or blank.";
        assert version >= LOWEST_REVISION_THATS_SAFE_TO_DELETE      : "version must be >= " + LOWEST_REVISION_THATS_SAFE_TO_DELETE;
        
        // Body
        log.debug("Purging version " + version + " from AVM store " + avmStoreId + ".");
        avmService.purgeVersion(version, avmStoreId);
    }


    /**
     * @see org.alfresco.extension.AVMPurgeVersionsService.AVMVersionPurgeService#purgeAllBefore(java.lang.String, int)
     */
    public int purgeAllBefore(String avmStoreId, int version)
    {
        int                     result         = 0;
        int                     currentVersion = 0;
        List<VersionDescriptor> versionHistory = null;
        
        // PRECONDITIONS
        assert avmStoreId != null && avmStoreId.trim().length() > 0 : "avmStoreId must not be null, empty or blank.";
        assert version >= LOWEST_REVISION_THATS_SAFE_TO_DELETE      : "version must be >= " + LOWEST_REVISION_THATS_SAFE_TO_DELETE;
        
        // Body
        log.debug("Purging all versions prior to " + version + " from AVM store " + avmStoreId + ".");
        
        currentVersion = avmService.getLatestSnapshotID(avmStoreId);
        versionHistory = avmService.getStoreVersions(avmStoreId);
        
        if (versionHistory != null)
        {
            for (VersionDescriptor versionDescriptor : versionHistory)
            {
                int versionNumber = versionDescriptor.getVersionID();
                
                if (versionNumber >= LOWEST_REVISION_THATS_SAFE_TO_DELETE && versionNumber < version && versionNumber < currentVersion)
                {
                    purge(avmStoreId, versionNumber);
                    result++;
                }
            }
        }
        
        log.debug("Purged " + result + " versions from AVM store " + avmStoreId + ".");
        
        return(result);
    }


    /**
     * @see org.alfresco.extension.AVMPurgeVersionsService.AVMVersionPurgeService#purgeAllBefore(java.lang.String, java.util.Date)
     */
    public int purgeAllBefore(String avmStoreId, Date timestamp)
    {
        int                     result             = 0;
        int                     currentVersion     = 0;
        List<VersionDescriptor> versionHistory     = null;
        Date                    exclusiveTimestamp = null;
        
        // PRECONDITIONS
        assert avmStoreId != null && avmStoreId.trim().length() > 0 : "avmStoreId must not be null, empty or blank.";
        assert timestamp != null                                    : "timestamp must not be null.";
        
        // Body
        log.debug("Purging all versions older than " + timestamp + " from AVM store " + avmStoreId + ".");
        
        exclusiveTimestamp = new Date(timestamp.getTime() - 1);
        currentVersion     = avmService.getLatestSnapshotID(avmStoreId);
        versionHistory     = avmService.getStoreVersions(avmStoreId, null, exclusiveTimestamp);
        
        if (versionHistory != null)
        {
            for (VersionDescriptor versionDescriptor : versionHistory)
            {
                int versionNumber = versionDescriptor.getVersionID();
                
                if (versionNumber >= LOWEST_REVISION_THATS_SAFE_TO_DELETE && versionNumber < currentVersion)
                {
                    purge(avmStoreId, versionNumber);
                    result++;
                }
            }
        }
        
        log.debug("Purged " + result + " versions from AVM store " + avmStoreId + ".");
        
        return(result);
    }

}
